package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.MyOrderMapper;
import com.chixing.mapper.StoreMapper;
import com.chixing.pojo.*;
import com.chixing.service.MyOrderService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/10/5 13:54
 */
@Service
public class MyOrderServiceImpl implements MyOrderService {
    @Autowired
    private MyOrderMapper myOrderMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private StoreMapper storeMapper;


    @Override
    public ServerResponse getById(Integer orderId) {
        MyOrder myOrder = myOrderMapper.selectByPrimaryKey(orderId);
        if(myOrder != null)
            return ServerResponse.getSuccess(myOrder);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getByOrderId(Integer myOrderId) {
        MyOrder myOrder = myOrderMapper.selectByPrimaryKey(myOrderId);
        Integer custId = myOrder.getCustId();
        Customer customer = customerMapper.selectByPrimaryKey(custId);
        Integer carId = myOrder.getCarId();
        Car car = carMapper.selectByPrimaryKey(carId);
        Integer storeBorrowId = myOrder.getStoreBorrowId();
        Store storeBorrow = storeMapper.selectByPrimaryKey(storeBorrowId);
        Integer storeReturnId = myOrder.getStoreReturnId();
        Store storeReturn = storeMapper.selectByPrimaryKey(storeReturnId);
        OrderCustomerCarStoreVO orderCustomerCarStore = new OrderCustomerCarStoreVO(myOrder, customer, car, storeBorrow, storeReturn);
        if(orderCustomerCarStore != null)
            return ServerResponse.getSuccess(orderCustomerCarStore);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCustomerOrder(Integer custId,Integer status) {
        List<MyOrderCarVO> myOrderCarVOS=new ArrayList<>();
        MyOrderExample myOrderExample = new MyOrderExample();
        if(status == null)
            myOrderExample.createCriteria().andCustIdEqualTo(custId);
        else
            myOrderExample.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(status);
        List<MyOrder> myOrders = myOrderMapper.selectByExample(myOrderExample);
        for (MyOrder myOrder:myOrders){
            Car car=carMapper.selectByPrimaryKey(myOrder.getCarId());
            myOrderCarVOS.add(new MyOrderCarVO(myOrder,car));
        }
        if (myOrderCarVOS.size()!=0)
            return ServerResponse.getSuccess(myOrderCarVOS);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse saveMyOrder(MyOrder myOrder) {
        int rows=myOrderMapper.insert(myOrder);
        if (rows>0)
            return ServerResponse.getSuccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getMyOrderStoreCar(Integer myOrderId) {
        MyOrderExample myOrderExample = new MyOrderExample();
        MyOrderExample.Criteria criteria = myOrderExample.createCriteria();
        criteria.andMyOrderIdEqualTo(myOrderId);
        MyOrder myOrder = myOrderMapper.selectByExample(myOrderExample).get(0);
        Store store=storeMapper.selectByPrimaryKey(myOrder.getStoreReturnId());
        Car car=carMapper.selectByPrimaryKey(myOrder.getCarId());
        MyOrderStoreCarVO myOrderStoreCarVO = new MyOrderStoreCarVO(myOrder,store,car);
        if (myOrderStoreCarVO!=null)
            return ServerResponse.getSuccess(myOrderStoreCarVO);
        return ServerResponse.getFail(null);
    }

    @Override
    public MyOrder getByOrderNumber(Long orderNumber) {
        MyOrderExample myOrderExample = new MyOrderExample();
        MyOrderExample.Criteria criteria = myOrderExample.createCriteria();
        criteria.andMyOrderNumberEqualTo(orderNumber);
        MyOrder myOrder = myOrderMapper.selectByExample(myOrderExample).get(0);
        return myOrder;
    }
    @Override
    public ServerResponse saveMyOrderInformation(MyOrder myOrder) {
        int rows=myOrderMapper.insert(myOrder);
        if (rows>0)
            return ServerResponse.getSuccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse updateCustomer(Customer customer) {
        int rows=customerMapper.updateByPrimaryKey(customer);
        if (rows>0)
            return ServerResponse.getSuccess(rows);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse updateOrderStatus4(MyOrder myOrder) {
        myOrder.setStatus(4);
        int update = myOrderMapper.updateByPrimaryKey(myOrder);
        if(update > 0){
            return ServerResponse.updateSuccess(myOrder);
        }
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse updateOrderStatus2(MyOrder myOrder) {
        myOrder.setStatus(2);
        int update = myOrderMapper.updateByPrimaryKey(myOrder);
        if(update > 0){
            return ServerResponse.updateSuccess(myOrder);
        }
        return ServerResponse.updateFail(null);
    }
}
